/*
 * @Author: your name
 * @Date: 2020-09-15 11:56:00
 * @LastEditTime: 2020-12-31 16:33:30
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \inventory-apie:\hjimi\人脸\html\face-recognition\src\store\index.js
 */
import Vue from "vue"
import Vuex from "vuex"
import { logout } from "@/api/user"
import { constantRoutes, resetRouter } from "@/router"
import router from "../router"
import { Message } from 'element-ui'
import cookies from 'js-cookie'
import { getToken } from '@/utils/auth'
// import qs from 'qs'
Vue.use(Vuex);

// 登录
const loginModule = {
  state: {
    nameReg: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{3,19}$/,
    pswReg: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/,
    User: cookies.get('User') ? cookies.get('User') : '',
    token: getToken() ? getToken() : 'token',
    permissions: 'null',
    imageList: []
  },
  mutations: {
//操作用户信息
    setUser(state, getUser) { //{User: res.User} //存储User
      state.User = getUser
      cookies.set("User",getUser)
    },
    delUser(state) { //删除User
      state.User = ''
      cookies.remove('User')
    },
    addImageList (state,imgeList) {
      state.imageList = imgeList
    },
//操作token
    setToken(state, getToken) {  //存储token
      let millisecond = new Date().getTime(),
          expiresTime = new Date(millisecond + 60 * 1000 * 1200) //2h后过期
          state.token = getToken
          cookies.set("token", getToken, {expires: expiresTime})
    },
    delToken(state) { //删除token
    state.token = ''
    cookies.remove('token')
    },
},
  getters: {
    permissions (state,val) {
     return state.permissions = '0'
   },
  //  User (state) {
  //    return state.User = eval('(' + state.User + ')')
  //  }
  },
  actions: {
    logout({ commit }) {
      return new Promise((resolved) => {
        logout().then((res) => {  
          if (res.data == null) {
             resolved();
          }
        });
      }).then((val)=> {
        commit('delToken','');
         resetRouter();
         Message({
          message: "已退出系统",
          type: "success"
        });
        router.push('/')
      })
    },
  },
}

// 场景应用
const liveApp = {
  state: {
    
  }
}
export default new Vuex.Store({
  modules: {
    loginModule: loginModule
  }
});
